﻿Imports AForge.Imaging.Filters
Imports AForge
Imports UCBase
Imports System.Drawing

Public Class Blue2BW
    Implements I_ImageExchange
    Implements I_LoadSave
    Private WithEvents SourceEvent As I_ImageExchange
    Private Backpic As New BackgroundCreator
    Private Mov As New Mover
    Private SourceBitmap As System.Drawing.Bitmap
    Private OverLay As Bitmap
    Private DestBitmap As System.Drawing.Bitmap

    Private ByPassed As Boolean = False
    Private Convert As New UCBase.ColorConverter
    Public Event ImageReady(ByVal Image As System.Drawing.Bitmap) Implements I_ImageExchange.ImageOut1

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.BackgroundImage = Backpic.CreatePic(Me.Width, Me.Height, Color.CadetBlue)
        Mov.Connect(Me)
    End Sub

    Private Sub SourceEvent_ImageReady(ByVal Image As System.Drawing.Bitmap) Handles SourceEvent.ImageOut1
        SourceBitmap = Convert.ConvertTo24Bit(Image)
        If ByPassed Then
            DestBitmap = SourceBitmap
            RaiseEvent ImageReady(DestBitmap)
        Else
            ProcFilter()
        End If
    End Sub
    Private Sub ProcFilter()
        Dim Tmp As Bitmap
        Dim filter As ExtractChannel = New ExtractChannel(AForge.Imaging.RGB.B)
        Tmp = filter.Apply(SourceBitmap)
        Dim filterInB As GrayscaleToRGB = New GrayscaleToRGB
        DestBitmap = filterInB.Apply(Tmp)
        RaiseEvent ImageReady(DestBitmap)
    End Sub

    Private Sub SocketIn_Connect() Handles SocketIn.Connect
        Dim S As Socket = SocketIn.Remote
        SourceEvent = S.Parent
    End Sub

    Private Sub BlankTransformFilter_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        '230, 105
        Me.Width = 230
        Me.Height = 105
    End Sub

    Private Sub Button3D5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3D5.Click
        If ByPassed = False Then
            ByPassed = True
            EnableLed.State = Led.LedState.Led_On
        Else
            ByPassed = False
            EnableLed.State = Led.LedState.Led_Off
        End If
    End Sub
    Public Function GetSettings() As String Implements UCBase.I_LoadSave.GetSettings
        Return ""
    End Function

    Public Sub SetSettings(ByVal Settings As String) Implements UCBase.I_LoadSave.SetSettings

    End Sub

End Class
